package com.tianzeculture.stagebao.activity.artist.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.ArtistEditDataActivity;
import com.tianzeculture.stagebao.activity.artist.ArtistEditPriceActivity;
import com.tianzeculture.stagebao.activity.artist.ArtistEditTagActivity;
import com.tianzeculture.stagebao.activity.artist.ExperienceActivity;
import com.tianzeculture.stagebao.activity.artist.MyArtistActivity;
import com.tianzeculture.stagebao.activity.artist.adapter.AddPriceAdapter;
import com.tianzeculture.stagebao.activity.business.RecruitDetailActivity;
import com.tianzeculture.stagebao.activity.personal.SelectJobActivity;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.ArtistsDetailedBean;
import com.tianzeculture.stagebao.bean.MyOfferBean;
import com.tianzeculture.stagebao.bean.PersonnelPoolBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.ArtistRestUsage;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;
import com.tianzeculture.stagebao.view.MyRecyclerview;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;

/**
 * Created by Administrator on 2018/6/7 0007.
 * 艺人详情页-资料
 */

public class MyArtistDataFragment extends BaseFragment {


//    DataAdapter dataAdapter = null;

    @BindView(R.id.btnSure)
    Button btnSure;
    @BindView(R.id.myRecyclerview)
    MyRecyclerview myRecyclerview;
    @BindView(R.id.tvClickData)
    TextView tvClickData;
    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvWeight)
    TextView tvWeight;
    @BindView(R.id.tvShoeSize)
    TextView tvShoeSize;
    @BindView(R.id.tvClickTag)
    TextView tvClickTag;
    @BindView(R.id.tagFlowLayout)
    TagFlowLayout tagFlowLayout;
    //    @BindView(R.id.tvStyleTag)
//    TextView tvStyleTag;
//    @BindView(R.id.tvAppearanceTag)
//    TextView tvAppearanceTag;
//    @BindView(R.id.tvShapeTag)
//    TextView tvShapeTag;
//    @BindView(R.id.tvCharmTag)
//    TextView tvCharmTag;
    @BindView(R.id.tvClickMoney)
    TextView tvClickMoney;
    @BindView(R.id.tvClickExperience)
    TextView tvClickExperience;
    @BindView(R.id.tvExperience)
    TextView tvExperience;
    Unbinder unbinder;
    ArtistsDetailedBean bean;
    User myUser = null;

    String style;
    String outward;
    String bodytype;

    String userHeight;
    String userWeight;
    String shoeSize;
    String myid;
    String experience;
    ArrayList<String> stringlist = new ArrayList<>();
    private int updateDataId = 5;
    AddPriceAdapter adapter;
    @Request
    ArtistRestUsage artistRestUsage = null;
    @Request
    UserRestUsage userRestUsage = null;
    List<String> strings = new ArrayList<>();
    List<String> arr = new ArrayList<>();

    private int offerDeleteId = 8;
    private int myInfoId = 18;

    @Override
    public void onAttach(Activity context) {
        super.onAttach(context);
        bean = ((MyArtistActivity) context).getData();
    }

    @Override
    public void initUI() {
        super.initUI();
        EventBus.getDefault().register(this);
        myid = bean.getDataList().get(0).getId();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myRecyclerview.setLayoutManager(linearLayoutManager);
        myRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        stringlist.clear();
        for (MyOfferBean offerBean : bean.getPriceList()) {
            stringlist.add(offerBean.getParentName() + "-" + offerBean.getName() + "---" + offerBean.getPrice() + "/" + offerBean.getUnit());
        }
//        if (!"".equals(tag) && tag != null) {
//            if (tag.contains(",")) {
//                String[] strs = tag.split(",");
//                for (String s : strs)
//                    stringlist.add(s);
//            } else
//                stringlist.add(tag);
//        }
        adapter = new AddPriceAdapter(R.layout.item_add_price, stringlist);
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                showNormalDialog(bean.getPriceList().get(position).getId());
                return true;
            }
        });
        myRecyclerview.setAdapter(adapter);
        if (bean.getDataList().size() == 0)
            return;
        tvHeight.setText("身高:" + bean.getDataList().get(0).getUserHeight() + "cm");
        tvWeight.setText("体重:" + bean.getDataList().get(0).getUserWeight() + "kg");
        tvShoeSize.setText("年龄:" + bean.getDataList().get(0).getShoeSize() + "岁");
        strings.clear();
        String tag1 = bean.getDataList().get(0).getTag1();
        strings.addAll(getTags(tag1));
        String tag2 = bean.getDataList().get(0).getTag2();
        strings.addAll(getTags(tag2));
        String tag3 = bean.getDataList().get(0).getTag3();
        strings.addAll(getTags(tag3));
        tagFlowLayout.setAdapter(new TagAdapter<String>(strings) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_characteristic,
                        tagFlowLayout, false);
                TextView tvCharacteristic = view.findViewById(R.id.tvCharacteristic);
                tvCharacteristic.setText(s);
                if (position % 4 == 0)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpink_round);
                else if (position % 4 == 1)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpurple_round);
                else if (position % 4 == 2)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightorange_round);
                else if (position % 4 == 3)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightgreen_round);
                return view;
            }
        });

        tvExperience.setText(bean.getDataList().get(0).getExperience());
    }

    private List<String> getTags(String tag) {
        List<String> list = new ArrayList<>();
        if (tag == null)
            return list;
        if (!"".equals(tag)) {
            if (tag.contains(",")) {
                String[] arr = tag.split(",");
                for (String str : arr) {
                    list.add(str);
                }
            } else {
                list.add(tag);
            }
        }
        return list;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my_artist;
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == updateDataId) {
            getActivity().finish();
        }
        if (taskId == offerDeleteId) {
            userRestUsage.myInfo(myInfoId, Util.UserId);
        }
        if (taskId == myInfoId) {
            if (msg.getObj() == null)
                return;
            bean = (ArtistsDetailedBean) msg.getObj();
            stringlist.clear();
            for (MyOfferBean offerBean : bean.getPriceList()) {
                stringlist.add(offerBean.getParentName() + "-" + offerBean.getName() + "---" + offerBean.getPrice() + "/" + offerBean.getUnit());
            }
            adapter.setNewData(stringlist);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PersonnelPoolBean event) {
        switch (event.getType()) {
            case 1://艺人资料
                tvHeight.setText("身高:" + event.getUserHeight() + "cm");
                tvWeight.setText("体重:" + event.getUserWeight() + "kg");
                tvShoeSize.setText("鞋码:" + event.getShoeSize() + "cm");
                userHeight = event.getUserHeight();
                userWeight = event.getUserWeight();
                shoeSize = event.getShoeSize();
                break;
            case 2://个人标签
                arr.clear();
                arr.addAll(getTags(event.getTag1()));
                arr.addAll(getTags(event.getTag2()));
                arr.addAll(getTags(event.getTag3()));
                tagFlowLayout.setAdapter(new TagAdapter<String>(arr) {
                    @Override
                    public View getView(FlowLayout parent, int position, String o) {
                        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_characteristic,
                                tagFlowLayout, false);
                        TextView tvCharacteristic = view.findViewById(R.id.tvCharacteristic);
                        tvCharacteristic.setText(o);
                        if (position % 4 == 0)
                            tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpink_round);
                        else if (position % 4 == 1)
                            tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpurple_round);
                        else if (position % 4 == 2)
                            tvCharacteristic.setBackgroundResource(R.drawable.bg_lightorange_round);
                        else if (position % 4 == 3)
                            tvCharacteristic.setBackgroundResource(R.drawable.bg_lightgreen_round);
                        return view;
                    }
                });
                style = event.getTag1();
                outward = event.getTag2();
                bodytype = event.getTag3();
                break;
            case 3://报价
                userRestUsage.myInfo(myInfoId, Util.UserId);
                break;
            case 4://工作经验
                tvExperience.setText(event.getExperience());
                experience = event.getExperience();
                break;
        }
    }


    @OnClick({R.id.tvClickData, R.id.tvClickTag, R.id.tvClickMoney, R.id.tvClickExperience, R.id.btnSure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvClickData:
                Intent intent1 = new Intent(getActivity(), ArtistEditDataActivity.class);
                myUser = Util.user;
                intent1.putExtra("user", myUser);
                startActivityForResult(intent1, Util.EDIT_ARTIST_BASIC);
                break;
            case R.id.tvClickTag:
                Intent intent3 = new Intent(getActivity(), ArtistEditTagActivity.class);
                startActivityForResult(intent3, Util.HOT_TAG);
                break;
            case R.id.tvClickMoney:
                Intent intent2 = new Intent(getActivity(), SelectJobActivity.class);
                intent2.putExtra("isEdit", true);
                startActivity(intent2);
                break;
            case R.id.tvClickExperience:
                Intent intent4 = new Intent(getActivity(), ExperienceActivity.class);
                startActivityForResult(intent4, Util.WORK_EXPERIENCE);
                break;
            case R.id.btnSure:
//                String money = null;
//                if (stringlist.size() > 0) {
//                    StringBuilder sb = new StringBuilder();
//                    for (int i = 0; i < stringlist.size(); i++) {
//                        if (i == stringlist.size() - 1) {
//                            sb.append(stringlist.get(i));
//                            continue;
//                        }
//                        sb.append(stringlist.get(i));
//                        sb.append(",");
//                    }
//                    money = sb.toString();
//                }
                Map<String, String> params = new HashMap<>();
                params.put("id", myid);
                params.put("userHeight", userHeight);
                params.put("userWeight", userWeight);
                params.put("shoeSize", shoeSize);
//                params.put("tag", money);
                params.put("tag1", style);
                params.put("tag2", outward);
                params.put("tag3", bodytype);
                params.put("experience", experience);
                artistRestUsage.updapteTalentPool(updateDataId, params);
                break;
        }
    }

    private void showNormalDialog(final String id) {
        /*
 @setIcon 设置对话框图标
         *
 @setTitle 设置对话框标题
         *
 @setMessage 设置对话框消息提示
         *
 setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder
                normalDialog =
                new AlertDialog.Builder(getActivity());
        normalDialog.setMessage("你要删除本条报价纪录吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface
                                                dialog, int which) {
                        artistRestUsage.offerDelete(offerDeleteId, id);
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface
                                                dialog, int which) {
                        //...To-do
                    }
                });
        normalDialog.show();
    }
}
